import { baseRequestClient, requestClient } from '#/api/request';
import type { console } from 'inspector';
import CryptoJS from 'crypto-js';
const secretKey = 'mySuperSecretKey123';
export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  const password = CryptoJS.AES.encrypt(data.password, secretKey).toString();
  return requestClient.get<AuthApi.LoginResult>('/api/user/login', {
    params: { username: data.username, password: password },
  });
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post(
    '/api/user/logout',
    {
      withCredentials: true,
    },
    {
      headers: {
        authorization: `${
          JSON.parse(localStorage.getItem('vben-web-ele-5.4.0-dev-core-access'))
            .accessToken
        }`,
      },
    },
  );
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}
